#include "imagebutton.h"

CImageButton::~CImageButton()
	{
	}

CImageButton::CImageButton(Context& context)
	: id(0)
	, left(0)
	, top(0)
	, width(0)
	, height(0)
	, mCurrBmp(NULL)
	, mDownBmp(NULL)
	, mDefaultBmp(NULL)
	, mDisableBmp(NULL)
	, mContext(context)
	, isDown(EFalse)
	, isEnable(ETrue)
	, mListener(NULL)
	{
	}

void CImageButton::SetOnClickListener(CImageButton::OnZoomClickListener& listener)
	{
	mListener = &listener;
	}

void CImageButton::SetBitmap(CFbsBitmap* defaultBmp, CFbsBitmap* downBmp, CFbsBitmap* disableBmp)
	{
    mDownBmp = downBmp;
    mDefaultBmp = defaultBmp;
    mDisableBmp = disableBmp;

    mCurrBmp = mDefaultBmp;
    width = mCurrBmp->SizeInPixels().iWidth;
    height = mCurrBmp->SizeInPixels().iHeight;
	}

void CImageButton::SetEnabled(TBool flag)
	{
    isEnable = flag;
    if (isEnable) {
        mCurrBmp = mDefaultBmp;
    } else {
        mCurrBmp = mDisableBmp;
    }
	}

void CImageButton::SetXY(float x, float y)
	{
    this->left = (int) x;
    this->top = (int) y;
	}

void CImageButton::OnDraw(CGraphicsProxy& canvas)
	{
	canvas.DrawImage(TPoint(left, top), mCurrBmp);
	}

int CImageButton::GetId() const
	{
	return id;
	}

void CImageButton::SetId(int id)
	{
	this.id = id;
	}

TBool CImageButton::OnTouchEvent(const MotionEvent& event)
	{
    int x = (int) event.GetX();
    int y = (int) event.GetY();
    
    if ((left <= x && x <= left + width + 5) && (top <= y && y <= top + height)) {
        if (!isEnable) {
        	isDown = true;
            return true;
        }
        switch (event.GetAction()) {
            case MotionEvent::EMotionActionDown:
            	isDown = true;
                mCurrBmp = mDownBmp;
                break;
            case MotionEvent::EMotionActionUp:
            	isDown = false;
            	if (mCurrBmp == mDownBmp){
                    mListener->OnZoomClick(id);
            	}
                break;
        }
        return true;
    }
    if (isEnable) {
    	mCurrBmp = mDefaultBmp;
    }

    if (event.GetAction() == MotionEvent::EMotionActionMove && isDown){
    	return true;
    }
    
    isDown = false;
    return false;
	}
